package com.xch.controller;

import com.xch.bean.Courier;
import com.xch.bean.Express;
import com.xch.bean.Message;
import com.xch.bean.ResultData;
import com.xch.mvc.ResponseBody;
import com.xch.service.CourierService;
import com.xch.service.ExpressService;
import com.xch.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class CourierController {

    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        //1.获取查询数据的起始索引值
        int offset = Integer.parseInt(request.getParameter("offset"));
        //2.获取当前页要查询的数据量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //3.进行查询
        List<Courier> list = CourierService.findAll(true,offset,pageNumber);
        ResultData<Courier> data = new ResultData<>();
        int total = CourierService.total();
        data.setRows(list);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        System.out.println(json);
        return json;
    }

    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest request,HttpServletResponse response){
        String courierName = request.getParameter("name");
        String courierPhone = request.getParameter("phone");
        String courierId = request.getParameter("id");
        String password = request.getParameter("password");

        Courier c = new Courier(courierName,courierPhone,courierId,password);

        boolean flag = CourierService.insert(c);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("快递录入成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("快递录入失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/find.do")
    public String find(HttpServletRequest request,HttpServletResponse response){
        String phone = request.getParameter("phone");
        Courier c = CourierService.findByPhone(phone);
        Message msg = new Message();
        if(c == null){
            msg.setStatus(-1);
            msg.setResult("号码不存在");
        }else{
            msg.setStatus(0);
            msg.setResult("已查找到");
            msg.setData(c);
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println(json);
        return json;
    }

    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request,HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String courierId = request.getParameter("courierId");
        String password = request.getParameter("password");
        Courier c = new Courier(name,phone,courierId,password);
        boolean flag = CourierService.update(id, c);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest request,HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = CourierService.delete(id);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("刪除成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("刪除失敗");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}
